var form;
var layer;
//页面初始化时加载Layui框架
layui.use(['form', 'layer', 'laydate'], function () {
    form = layui.form();
    layer = layui.layer;
    //关闭
    var closed = $(".close");
    closed.click(function () {
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index); //再执行关闭
    });
    //监听提交
    form.on('submit(submit)', function () {
        //提交按钮置为不可用
        $("#submit").addClass("layui-btn-disabled");
        addOrUpdateDict();
    });
});

/**
 * 新增/修改
 */
function addOrUpdateDict() {
    var dictId = $.trim($("#dictId").val());
    // 名称
    var dictName = $.trim($("#dictName").val());
    // 值
    var dictValue = $.trim($("#dictValue").val());
    // 类型
    var dictType = $.trim($("#dictType").val());
    // 备注
    var remark = $("#remark").val();

    var type = $("#type").val();
    if (null == type || '' == type) {
        type = 'add';
    }

    var paramData = {
        "dict.id": dictId,
        "dict.name": dictName,
        "dict.value": dictValue,
        "dict.type": dictType,
        "dict.remark": remark,
        "type": type
    };
    $.ajax({
        type: 'post',
        url: basePath + "dict/saveDict.action",
        data: paramData,
        dataType: 'text',
        success: function (data) {
            if (data != null && data != "" && data == "success") {
                closeAndRefresh();
            } else if (data == "exist") {
                layer.open({
                    title: '提示',
                    icon: 0,
                    content: '同一种数据字典类型下，必须保证名称和值的唯一性!'
                });
            } else {
                var content = '';
                if (type == "add") {
                    content = '新增失败!';
                } else {
                    content = '修改失败!';
                }
                layer.open({
                    title: '错误',
                    icon: 5,
                    content: content,
                    yes: function () {
                        closeAndRefresh();
                    }
                });
            }
            //提交按钮置为可用
            $("#submit").removeClass("layui-btn-disabled");
        }
    });
}